package com.bac.rentmap.ad.flat.rating.item;

import com.bac.rentmap.ad.flat.FlatAd;
import com.bac.rentmap.ad.flat.image.FlatImageType;

public class FlatImageRatingItemCalculator implements RatingItemCalculator {
    private static final int MIN_POSSIBLE_RATING = 0;
    private static final int MIN_RATING_INCREMENT = 60;
    private static final int FLAT_IMAGE_RATIO = 10;

    @Override
    public long calculate(FlatAd flatAd) {
        int imageCount = 0;
        if(flatAd.getExternal()){
            if(flatAd.getExternalImages() != null){
                imageCount = flatAd.getExternalImages().size();    
            }
        }
        else if (flatAd.getFlatImages() != null){
            imageCount = flatAd.getFlatImages().size();
        }

        if (imageCount == 0)
            return getMinPossibleRating();
        return calculateRating(imageCount);
    }

    private int calculateRating(int imageCount) {
        return MIN_RATING_INCREMENT + FLAT_IMAGE_RATIO * imageCount;
    }

    @Override
    public long getMaxPossibleRating() {
        return calculateRating(FlatImageType.values().length);
    }

    @Override
    public long getMinPossibleRating() {
        return MIN_POSSIBLE_RATING;
    }

}
